package com.lsy.weizhi2.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.SelfMessageList;
import com.lsy.weizhi2.JsonObject.UserInfo;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.ActivityList;
import com.lsy.weizhi2.activity.CollectManager;
import com.lsy.weizhi2.activity.CompleteInfo;
import com.lsy.weizhi2.activity.ConcludeManager;
import com.lsy.weizhi2.activity.ConnectService;
import com.lsy.weizhi2.activity.FriendCard;
import com.lsy.weizhi2.activity.LoginActivty;
import com.lsy.weizhi2.activity.MyFollow;
import com.lsy.weizhi2.activity.MyGoldMaster;
import com.lsy.weizhi2.activity.MySelfMessage;
import com.lsy.weizhi2.activity.MySetting;
import com.lsy.weizhi2.activity.WeiZhiManager;
import com.lsy.weizhi2.activity.YueDanManager;
import com.lsy.weizhi2.adapter.PersonGridViewAdapter;
import com.lsy.weizhi2.config.CeShi;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.CostomFlag;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.myutils.ActivityItem;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.myutils.TitleViewCall;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


/**
 * Created by 嗨哟 on 2017/3/13.
 */
public class PersonFragment extends Fragment implements View.OnClickListener {
    private com.lsy.weizhi2.mybase.MyTitleView mPerson_frag_title;
    private ImageView mPerson_head_image;
    private LinearLayout mPerson_ll1;
    private TextView mPerson_name;
    private TextView mPerson_renzheng;
    private TextView mPerson_complete;
    private LinearLayout mPerson_ll2;
    private RatingBar mPerson_dengji;
    private TextView mPerson_classify;
    private ImageView mBashou_1;
    private ImageView mBashou_2;
    private com.lsy.weizhi2.customview.Person_Num mPerson_renqi;
    private com.lsy.weizhi2.customview.Person_Num mPerson_fensi;
    private com.lsy.weizhi2.customview.Person_Num mPerson_guanzhu;
    private com.lsy.weizhi2.customview.Person_Num mPerson_jinzhu;
    private com.lsy.weizhi2.customview.Person_Num mPerson_jinku;
    private GridView mPerson_gv;
    private Context context;
    private PersonGridViewAdapter adapter;
    private Handler handler = new Handler();
    private int xx;
    private Gson gson;
    private String name;
    private int image;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.person_fragment, container, false);
        bindViews(view);
        return view;
    }

    private void bindViews(View view) {
        mPerson_frag_title = (com.lsy.weizhi2.mybase.MyTitleView) view.findViewById(R.id.person_frag_title);
        mPerson_head_image = (ImageView) view.findViewById(R.id.person_head_image);
        mPerson_ll1 = (LinearLayout) view.findViewById(R.id.person_ll1);
        mPerson_name = (TextView) view.findViewById(R.id.person_name);
        mPerson_renzheng = (TextView) view.findViewById(R.id.person_renzheng);
        mPerson_complete = (TextView) view.findViewById(R.id.person_complete);
        mPerson_ll2 = (LinearLayout) view.findViewById(R.id.person_ll2);
        mPerson_dengji = (RatingBar) view.findViewById(R.id.person_dengji);
        mPerson_classify = (TextView) view.findViewById(R.id.person_classify);
        mBashou_1 = (ImageView) view.findViewById(R.id.bashou_1);
        mBashou_2 = (ImageView) view.findViewById(R.id.bashou_2);
        mPerson_renqi = (com.lsy.weizhi2.customview.Person_Num) view.findViewById(R.id.person_renqi);
        mPerson_fensi = (com.lsy.weizhi2.customview.Person_Num) view.findViewById(R.id.person_fensi);
        mPerson_guanzhu = (com.lsy.weizhi2.customview.Person_Num) view.findViewById(R.id.person_guanzhu);
        mPerson_jinzhu = (com.lsy.weizhi2.customview.Person_Num) view.findViewById(R.id.person_jinzhu);
        mPerson_jinku = (com.lsy.weizhi2.customview.Person_Num) view.findViewById(R.id.person_jinku);
        mPerson_gv = (GridView) view.findViewById(R.id.person_gv);
        mPerson_renqi.setOnClickListener(this);
        mPerson_fensi.setOnClickListener(this);
        mPerson_guanzhu.setOnClickListener(this);
        mPerson_jinzhu.setOnClickListener(this);
        mPerson_jinku.setOnClickListener(this);
        mPerson_complete.setOnClickListener(this);
        mPerson_frag_title.setTitleViewCall(new TitleViewCall() {
            @Override
            public void Click(View view, int flag) {
                if (flag == TitleViewCall.MORE) {
                    MyMethod.toActivity(context, MySetting.class);
                }
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        gson = new Gson();
        adapter = new PersonGridViewAdapter(context, null);
        mPerson_gv.setAdapter(adapter);
        mPerson_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (CeShi.person_name[i].equals("我的帖子")) {
                    MyMethod.toActivity(context, WeiZhiManager.class);
                } else if (CeShi.person_name[i].equals("约单管理")) {
                    MyMethod.toActivity(context, YueDanManager.class);
                } else if (CeShi.person_name[i].equals("我的活动")) {
                    CostomFlag.ActivityList_Flag = ActivityItem.Activity_Me;
                    MyMethod.toActivity(context, ActivityList.class);
                } else if (CeShi.person_name[i].equals("我的名片")) {
                   // MyInfo.CartName = "我的名片";
                   // MyMethod.toActivity(context, FriendCard.class);
                } else if (CeShi.person_name[i].equals("我收藏的")) {
                    MyMethod.toActivity(context, CollectManager.class);
                } else if (CeShi.person_name[i].equals("成交提醒")) {
                    //MyMethod.toActivity(context, ConcludeManager.class);
                } else if (CeShi.person_name[i].equals("我的相册")) {

                } else if (CeShi.person_name[i].equals("我关注的")) {

                } else if (CeShi.person_name[i].equals("联系客服")) {
                    MyMethod.toActivity(context, ConnectService.class);
                } else if (CeShi.person_name[i].equals("我的私信")) {
                    MyMethod.toActivity(context, MySelfMessage.class);
                }

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = mPerson_frag_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mPerson_frag_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                mPerson_frag_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
        GetInfo();
        getData();
    }

    private void getData(){
        MyOkHttpUtils.getInstance(context).doGet(Constants.SendPrivateMessage,null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ReadFragment","message==="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){

                                    SelfMessageList selfMessageList=gson.fromJson(message,SelfMessageList.class);
                                    List<SelfMessageList.ListBean> list=selfMessageList.getList();
                                    for (int i = 0; i < list.size(); i++) {
                                        if(list.get(i).getStatus()==0){//遍历所有，有一个就显示红点
                                            adapter.UpData(null,true);
                                        }
                                    }
                                }else{
                                    String msg=jsonObject.getString("message");
                                    if(msg.equals("错误的认证Token")){
                                        MyMethod.ShowDialogLogin(context,LoginActivty.class);
                                    }
                                    if(MyInfo.Token==null){
                                        MyMethod.toActivity(context,LoginActivty.class);
                                        ((Activity)context).finish();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    public void GetInfo() {
        MyOkHttpUtils.getInstance(context).doGet(Constants.GetInfo, null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if (message != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if (result) {
                                    UserInfo userInfo = gson.fromJson(message, UserInfo.class);
                                    name = userInfo.getData().getRealName();

                                    mPerson_name.setText(name);
                                    image = userInfo.getData().getAvatar();
                                    Glide.with(context)
                                            .load(new GlideUrl(Constants.DOWN_USER_PICTURE + image, new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID", MyInfo.User_Id + "").build()))
                                            .transform(new GlideCircleTransform(context))
                                            .error(R.mipmap.default_head)
                                            .into(mPerson_head_image);
                                }else{
                                    String msg=jsonObject.getString("message");
                                    if(msg.equals("错误的认证Token")){
                                        MyMethod.ShowDialogLogin(context,LoginActivty.class);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.person_renqi:
                break;
            case R.id.person_fensi:
                //MyMethod.toActivity(context, MyFollow.class);
                break;
            case R.id.person_guanzhu:
                break;
            case R.id.person_jinzhu:
                //MyMethod.toActivity(context, MyGoldMaster.class);
                break;
            case R.id.person_jinku:
                break;
            case R.id.person_complete:
                MyMethod.toActivity(context, CompleteInfo.class);
                break;
        }
    }
}
